package com.hjk.common.utils.reflect;

import org.junit.Test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author huangjunkai
 * @version 1.0
 * @date 2020/5/27 20:29
 */
public class RefiectUtils {
    /**
     * 一直往上到Object,获取这个对象及其父类的属性
     * @param clazz
     * @return
     */
    public static List<Field> getFields(Class<?> clazz) {
        Class<?> tempClass = clazz;
        ArrayList alField;
        for (alField = new ArrayList(); tempClass != Object.class; tempClass = tempClass.getSuperclass()) {
            Field[] fields = tempClass.getDeclaredFields();
            if (fields != null && fields.length > 0) {
                Field[] var4 = fields;
                int var5 = fields.length;
                for (int var6 = 0; var6 < var5; ++var6) {
                    Field field2 = var4[var6];
                    alField.add(field2);
                }
            }
        }
        return alField;
    }
    /**
     *
     */
    @Test
    public void test(){
        Arrays.stream(b.class.getDeclaredFields()).forEach(System.out::println);

        RefiectUtils.getFields(b.class).stream().forEach(System.out::println);

    }

}

class a{
    String a;
    String c;
    String b;
}

class b extends a{
    String d;
}
